/******************************************************************************
 * Copyright (C) 2014 ShenZhen YiHua Computer Co., Ltd.
 * All Rights Reserved.
 * 本软件为深圳怡化电脑股份有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体
 * 不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.core.util;
/**    
 * @description  :  
 * 				 : 时间工具类,提供各种时间转行API
 * @version      : V 1.0
 * @author       : ztiny
 * @create       : 2012-4-27 下午5:16:03  
 * @Copyright    : broadengate Corporation 2012    
 * 
 * Modification History:
 * 	Date			Author			Version			Description
 *--------------------------------------------------------------
 * 2012-4-27 下午5:16:03
 */
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 日期工具类
 * @author  Simon.Hoo(Ask@China-Loong.com)
 * @date    2014年3月24日
 * @version v1.0
 */
public class CalendarUtils extends org.apache.commons.lang.time.DateUtils {
	private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
	
	/**
	 * 日期格式化
	 */
	public static final SimpleDateFormat FORMAT_DATE = new SimpleDateFormat(
			"yyyy-MM-dd");

	/**
	 * 年月格式化
	 */
	public static final SimpleDateFormat FORMAT_YEAR_MONTH = new SimpleDateFormat(
			"yyyy-MM");
	
	/**
	 * 年月格式化
	 */
	public static final SimpleDateFormat FORMAT_YEAR= new SimpleDateFormat("yyyy");

	/**
	 * 月日格式化
	 */
	public static final SimpleDateFormat FORMAT_MONTH_DAY = new SimpleDateFormat(
			"MM-dd");

	public static final SimpleDateFormat FORMAT_AAA_TIME_NOT_SECOND = new SimpleDateFormat(
			"aaa HH:mm");

	/**
	 * 时间格式化(不含秒)
	 */
	public static final SimpleDateFormat FORMAT_TIME_NOT_SECOND = new SimpleDateFormat(
			"HH:mm");

	/**
	 * 时间格式化
	 */
	public static final SimpleDateFormat FORMAT_TIME = new SimpleDateFormat(
			"HH:mm:ss");

	/**
	 * 日期时间格式化(不含秒)
	 */
	public static final SimpleDateFormat FORMAT_DATE_TIME_NOT_SECOND = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm");

	/**
	 * 日期格式化字符串
	 */
	public static final SimpleDateFormat FORMAT_DATE_TIME = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	/**
	 * 将时间格式化日期格式(yyyy-MM-dd)
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date) {
		return formatDateByFormat(date, FORMAT_DATE);
	}

	public static String formatMonthDay(Date date) {
		return formatDateByFormat(date, FORMAT_MONTH_DAY);
	}
	
	/**
	 * 将日期格式化为年月(yyyy-MM)
	 * 
	 * @param date
	 * @return
	 */
	public static String formatYearMonth(Date date){
		return formatDateByFormat(date,FORMAT_YEAR_MONTH);
	}
	
	public static Integer formatYear(Date date){
		return Integer.valueOf(formatDateByFormat(date,FORMAT_YEAR));
	}
	/**
	 * 将时间格式为时间形式(HH:mm:ss)
	 * 
	 * @param date
	 * @return
	 */
	public static String formatTime(Date date) {
		return formatDateByFormat(date, FORMAT_TIME);
	}

	/**
	 * 将时间格式化为不带秒的时间形式(HH:mm)
	 * 
	 * @param date
	 * @return
	 */
	public static String formatTimeNoSecond(Date date) {
		return formatDateByFormat(date, FORMAT_TIME_NOT_SECOND);
	}

	public static String formatAaaTimeNoSecond(Date date) {
		return formatDateByFormat(date, FORMAT_AAA_TIME_NOT_SECOND);
	}

	/**
	 * 将时间格式化为日期时间格式(yyyy-MM-dd HH:mm:ss)
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDateTime(Date date) {
	    SimpleDateFormat format = new SimpleDateFormat(DEFAULT_PATTERN);
		return formatDateByFormat(date, format);
	}

	/**
	 * 将时间格式化为日期时间格式不带秒(yyyy-MM-dd HH:mm)
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDateTimeNotSecond(Date date) {
		return formatDateByFormat(date, FORMAT_DATE_TIME_NOT_SECOND);
	}
	
    /**
     * 
     * @Title: getNianJia
     * @Description: TODO(获取折算后年假的天数)
     * @param date 入职时间
     * @param num 年假额度
     * @return
     * @return double 返回类型 折算后的年假天数
     */
    public static double getNianJia(Date startDate,Date endDate, double num)
    {
        double returnNum;
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(CalendarUtil.getYear(date) + 1, 0, 1, 0, 0, 0);
        int intervalDays = CalendarUtils.getIntervalDays(startDate, endDate)+1;//包含收尾两天
        int dayOfYear = 0;
        if (CalendarUtils.isLeapYear(CalendarUtils.getYear(startDate)))
        {
            dayOfYear = 366;

        }
        else
        {
            dayOfYear = 365;
        }
        double temp = intervalDays * num / dayOfYear;// 折算后年假天数
        double decimal =temp-((int)(intervalDays * num)) / dayOfYear;//获得年假小数点后的值  不含整数
        if(decimal>=0.5){
        	returnNum=((int)(intervalDays * num)) / dayOfYear+0.5;
        }else{
        	returnNum=((int)(intervalDays * num)) / dayOfYear;
        }
        return returnNum;
    }

    /**
     * 
     * @Title: getIntervalDays
     * @Description: TODO(比较两个日期相差天数)
     * @param startDate
     * @param endDate
     * @return
     * @return int 返回类型
     */
    public static int getIntervalDays(Date startDate, Date endDate)
    {
        /*Calendar aCalendar = Calendar.getInstance();

        aCalendar.setTime(startDate);

        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);

        aCalendar.setTime(endDate);

        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);

        return day2 - day1;*/
        Calendar aCalendar = Calendar.getInstance();

        aCalendar.setTime(startDate);

        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
        int year1 = aCalendar.get(Calendar.YEAR);

        aCalendar.setTime(endDate);

        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
        int year2 = aCalendar.get(Calendar.YEAR);
        
        int minYear = year1 > year2?year2: year1;
        int maxYear = year1 > year2?year1: year2;
        int difference = 0;
        
        while(minYear != maxYear) {
            if(minYear%4==0 && minYear%100!=0 || minYear%400==0)
                difference += 366;
            else
                difference += 365;
            minYear ++;
        }
        if(endDate.getTime() > startDate.getTime())
            difference = ( day2 - day1) + difference;
        else 
            difference = ( day2 - day1) - difference;
        return difference;
    }

    /**
     * 
     * @Title: isLeapYear
     * @Description: TODO(判断某年是否为闰年)
     * @param year
     * @return
     * @return boolean 返回类型
     */
    public static boolean isLeapYear(int year)
    {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
            return true;
        else
            return false;
    }
    
    /**
     * 
     * @Title: getAge 
     * @Description: TODO(根据出生日期得到年龄) 
     * @param birthday
     * @return
     * @return int    返回类型
     */
    public static int getAge(Date birthday){
        
        Calendar aCalendar = Calendar.getInstance();

        aCalendar.setTime(birthday);

        int year1 = aCalendar.get(Calendar.YEAR);

        aCalendar.setTime(new Date());

        int year2 = aCalendar.get(Calendar.YEAR);

        return year2 - year1;
    }
    
    /**
     * 
     * @Title: getAge 
     * @Description: TODO(根据出生日期得到准确年龄--周岁) 
     * @param birthday
     * @return
     * @return double    返回类型
     */
    public static double getAccurateAge(Date birthday){
        
    	Calendar c1 = Calendar.getInstance();
    	
        Calendar c2 = Calendar.getInstance();
        
        String beginTime = String.valueOf(formatDate(birthday));
        try {
			c1.setTime(CalendarUtils.FORMAT_DATE.parse(beginTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        
        c2.setTime(new Date());
        
        return (float)CalendarUtils.diffDate(c2.getTime(), c1.getTime())/365;
    }
	/**
	 * 将时间格式化为指定格式
	 * 
	 * @param date
	 * @param formatformatDateByFormat
	 * @return
	 */
	public final static String formatDateByFormat(Date date,
			SimpleDateFormat format) {
		String result = "";
		if (date != null) {
			result = format.format(date);
		}
		return result;
	}

	/**
	 * 将java.util.Date转化为java.sql.Date
	 * 
	 * @param date
	 * @return
	 */
	public static java.sql.Date parseSqlDate(Date date) {
		if (date == null) {
			throw new NullPointerException();
		}
		return new java.sql.Date(date.getTime());
	}

	/**
	 * 获取年份
	 * 
	 * @param date
	 * @return
	 */
	public static int getYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.YEAR);
	}

	/**
	 * 获取月份
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取日期(一个月中的哪一天)
	 * 
	 * @param date
	 * @return
	 */
	public static int getDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.DAY_OF_MONTH);
	}
	
	/**
	 * 
	 * @Title: getChinaDate 
	 * @Description: TODO(获取中文年月日的日期) 
	 * @param date
	 * @return
	 * @return String    返回类型
	 */
	public static String getChinaDate(Date date) {
	    int year=getYear(date);
	    int month=getMonth(date);
	    int day=getDay(date);
	    String chinaDate=year+"年"+month+"月"+day+"日";
        return chinaDate;
    }
	
	/**
	 * 
	 * @Title: getChinaDateTime 
	 * @Description: TODO(获取中文年月日时分的日期) 
	 * @param date
	 * @return
	 * @return String    返回类型
	 */
	public static String getChinaDateTime(Date date) {
	    int year=getYear(date);
	    int month=getMonth(date);
	    int day=getDay(date);
	    int hour =getHour(date);
	    int minute =getMinute(date);
	    String chinaDate=year+"年"+month+"月"+day+"日  "+hour+"时："+minute+"分";
        return chinaDate;
    }
	
	/**
	 * 获取小时(24小时制)
	 * 
	 * @param date
	 * @return
	 */
	public static int getHour(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 获取分钟
	 * 
	 * @param date
	 * @return
	 */
	public static int getMinute(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MINUTE);
	}

	/**
	 * 获取秒
	 * 
	 * @param date
	 * @return
	 */
	public static int getSecond(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.SECOND);
	}

	/**
	 * 获取毫秒
	 * 
	 * @param date
	 * @return
	 */
	public static long getMillis(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getTimeInMillis();
	}

	/**
	 * 获取指定时间是星期几
	 * 
	 * @param date
	 * @return
	 */
	public static int getWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		dayOfWeek = dayOfWeek - 1;
		if (dayOfWeek == 0) {
			dayOfWeek = 7;
		}
		return dayOfWeek;
	}

	/**
	 * 日期相加
	 * 
	 * @param date
	 *            Date
	 * @param day
	 *            int
	 * @return Date
	 */
	public static Date addDate(Date date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
		return c.getTime();
	}
	
	public static Date addMinute(Date date, int minute) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) + ((long) minute) * 60 * 1000);
		return c.getTime();
	}
	
	@SuppressWarnings("deprecation")
	public static Date formtMinute(Date date, int interval) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) - ((long) date.getMinutes()%interval) * 60 * 1000);
		return c.getTime();
	}
	
	/** 
	* 获得指定日期的前几天 
	* @param date       某一天  
	* @param num   	前多少天
	* @param type  1=传入日期date的前num天；2=传入日期date的后num天
	* @retuen Date
	* @throws Exception 
	*/ 
	public static Date caleDateByNum(Date date,int num,int type){ 
		Calendar c = Calendar.getInstance(); 
		c.setTime(date); 
		int day=c.get(Calendar.DATE); 
		if(type==1){
			c.set(Calendar.DATE,day-num);
		}else{
			c.set(Calendar.DATE,day+num);
		}
		return c.getTime(); 
	} 

	public static void main(String[] args) {
		String d="2013-05-11";
		SimpleDateFormat ss=new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			Date a=caleDateByNum(ss.parse(d),365*12,2);
			String sssss=ss.format(a);
			System.out.println(sssss);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 日期相减
	 * 
	 * @param date
	 *            Date
	 * @param date1
	 *            Date
	 * @return int
	 */
	public static int diffDate(Date date, Date date1) {
		return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
	}
	public static int diffMinute(Date endDate, Date starDate) {
		return (int) ((getMillis(endDate) - getMillis(starDate)) / (60 * 1000));
	}
	
	

	/**
	 * 将字符串转换为日期格式的Date类型(yyyy-MM-dd)
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDate(String date) throws ParseException {
		return FORMAT_DATE.parse(date);
	}

	/**
	 * 将字符串转换为日期格式的Date类型(yyyy-MM)
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date parseMonth(String date) throws ParseException {
		return FORMAT_YEAR_MONTH.parse(date);
	}

	/**
	 * 将字符串转换为日期时间格式的Date类型(yyyy-MM-dd HH:mm:ss)
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDateTime(String date) throws ParseException {
		return FORMAT_DATE_TIME.parse(date);
	}
	
	public static Date parseDateTimeNoSecond(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.parse(date);
	}

	public static boolean compareDateAndWeekDay(Date date, int weekDay) {
		if (weekDay == getWeek(date)) {
			return true;
		}
		return false;
	}

	/**
	 * 将字符串转换为日期时间格式的Date类型( MM-dd )
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date parseMonthDay(String date) throws ParseException {
		return FORMAT_MONTH_DAY.parse(date);
	}

	/**
	 * 将字符串转为时间格式的Date类型(HH:mm)
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date parseTimeNoSecond(String date) throws ParseException {
		return FORMAT_TIME_NOT_SECOND.parse(date);
	}

	/**
	 * 将字符串转为时间格式的Date类型(YYYY)
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date parseTimeFromToDate(String date){
		Date d= null;
		try{
			d = FORMAT_YEAR.parse(date);
		}catch(ParseException ex){
			ex.printStackTrace();
		}
		return d;
	}
	
	/**
	 * 获得当前时间
	 * 
	 * @return
	 */
	public static Date getCurrentDateTime() {
		Date date = new Date();
		try {
	        String result = CalendarUtils.formatDateTime(date);
		    SimpleDateFormat format = new SimpleDateFormat(DEFAULT_PATTERN);
			return format.parse(result);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 时间比较yyyy-MM-dd
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean compareDate(Date date1, Date date2) {
		String str1 = formatDate(date1);
		String str2 = formatDate(date2);
		if (str1.equals(str2)) {
			return true;
		}
		return false;
	}

	/**
	 * 时间比较HH:mm
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean compareDateTimeHHmm(Date date1, Date date2) {
		String str1 = formatTimeNoSecond(date1);
		String str2 = formatTimeNoSecond(date2);
		if (str1.equals(str2)) {
			return true;
		}
		return false;
	}

	/**
	 * 判断字符串是否是有效的日期，
	 *  yyyy-MM-dd, yyyy-MM-d, yyyy-M-dd, yyyy-M-d yyyy/MM/dd
	 *  yyyy/MM/d   yyyy/M/dd yyyy/M/d   yyyyMMdd
	 * 
	 * @param date
	 *            日期字符串
	 * @return 是则返回true，否则返回false
	 */
	public static boolean isValidDate(String date) {
		if ((date == null) || (date.length() < 8)) {
			return false;
		}
		try {
			boolean result = false;
			SimpleDateFormat formatter;
			char dateSpace = date.charAt(4);
			String format[];
			if ((dateSpace == '-') || (dateSpace == '/')) {
				format = new String[4];
				String strDateSpace = Character.toString(dateSpace);
				format[0] = "yyyy" + strDateSpace + "MM" + strDateSpace + "dd";
				format[1] = "yyyy" + strDateSpace + "MM" + strDateSpace + "d";
				format[2] = "yyyy" + strDateSpace + "M" + strDateSpace + "dd";
				format[3] = "yyyy" + strDateSpace + "M" + strDateSpace + "d";
			} else {
				format = new String[1];
				format[0] = "yyyyMMdd";
			}

			for (int i = 0; i < format.length; i++) {
				String aFormat = format[i];
				formatter = new SimpleDateFormat(aFormat);
				formatter.setLenient(false);
				String tmp = formatter.format(formatter.parse(date));
				if (date.equals(tmp)) {
					result = true;
					break;
				}
			}
			return result;
		} catch (ParseException e) {
			return false;
		}
	}

	public static SimpleDateFormat getSimpleDateFormat(String pattern) {
		return new SimpleDateFormat(pattern);
	}
	
	/**
	  * @param time
	  * @param pattern
	  * @return
	  */
	 public static String timestampTostr(Timestamp time, String pattern) {
	  if (time == null) {
	   throw new IllegalArgumentException("Timestamp is null");
	  }
	  if (pattern != null && !"".equals(pattern)) {
	   if (!"yyyyMMddHHmmss".equals(pattern)
	     && !"yyyy-MM-dd HH:mm:ss".equals(pattern)
	     && !"yyyy-MM-dd".equals(pattern)
	     && !"MM/dd/yyyy".equals(pattern)){
	    throw new IllegalArgumentException("Date format ["+pattern+"] is invalid");
	   }
	  }else{
	   pattern = DEFAULT_PATTERN;
	  }
	  
	  Calendar cal = Calendar.getInstance(TimeZone.getDefault());
	  cal.setTime(time);
	  SimpleDateFormat sdf = new SimpleDateFormat(pattern);
	  return sdf.format(cal.getTime());
	 }

	 public static Timestamp strToTimestamp(String timeStr,String pattern)
	 {
	  Timestamp result = null;
	  if (timeStr == null) {
	   throw new IllegalArgumentException("Timestamp is null");
	  }
	  if (pattern != null && !"".equals(pattern)) {
	   if (!"yyyyMMddHHmmss".equals(pattern)
	     && !"yyyy-MM-dd HH:mm:ss".equals(pattern)
	     && !"MM/dd/yyyy HH:mm:ss".equals(pattern)
	     && !"yyyy-MM-dd".equals(pattern)
	     && !"MM/dd/yyyy".equals(pattern)) {
	    throw new IllegalArgumentException("Date format ["+pattern+"] is invalid");
	   }
	  }else{
	   pattern = DEFAULT_PATTERN;
	  }
	  
	  Date d = null;
	  SimpleDateFormat sdf = new SimpleDateFormat(pattern);
	  try {
	   d = sdf.parse(timeStr);
	   result = new Timestamp(d.getTime());
	  } catch (Exception e) {
	   e.printStackTrace();
	  }
	  return result;
	 }
	 
	 /** 
	  * 将timestamp转换成date 
	  * @author samsonYu 
	  * @param tt 
	  * @return 
	  */  
	 public static Date timestampToDate(Timestamp tt){  
	     return new Date(tt.getTime());  
	 }  
	 
	/*public static void main(String[] args) {
		try {
			Date d1 = parseDate("2000-12-10");
			Date d2 = parseDate("1900-12-10");
			System.out.println("ccc:"+CalendarUtils.parseDateTimeNoSecond("2013-08-30 00:00:00").toString());
			System.out.println(CalendarUtils.diffDate(d1, d2));
//			Date date = CalendarUtils.getCurrentDateTime();
//			System.out.println(date);
			System.out.println(strToTimestamp("2008-01-25 13:10:00","yyyy-MM-dd HH:mm:ss"));
			  System.out.println(timestampTostr(new Timestamp(System.currentTimeMillis()),"yyyy-MM-dd HH:mm:ss"));
			  System.out.println(strToTimestamp("01/25/2008","MM/dd/yyyy"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
*/
	 /*判断两个时间段是否有重叠 存在重叠返回true 适用岗位津贴规则*/
	 public static boolean isOverlap(Date leftStartDate, Date leftEndDate,  
			 Date rightStartDate, Date rightEndDate) {  
		 boolean isOk=false;
		 try {
			 if(leftEndDate==null){
				leftEndDate=parseDate("9999-12-31");
			 }
			 if(rightEndDate==null){
				 rightEndDate=parseDate("9999-12-31");
			 }
		 } catch (ParseException e) {
			 e.printStackTrace();
		 }
		/* if(leftEndDate.getTime()==rightStartDate.getTime()){
			 isOk= ((rightEndDate.getTime() >= leftStartDate.getTime())   
	                    && rightStartDate.getTime() < leftEndDate.getTime())  ;
		 }else{*/
			 isOk= ((leftEndDate.getTime() >= rightStartDate.getTime())   
	                    && leftStartDate.getTime() <= rightEndDate.getTime())  ;
		/* }
		 if(leftStartDate.getTime()==leftEndDate.getTime() &&
				 leftEndDate.getTime()==rightStartDate.getTime()&&
				 rightStartDate.getTime()==rightEndDate.getTime()){
			 isOk=true;
		 }*/
	        return  isOk;
	    } 
	 
	 /*判断两个时间段间隔天数（必须为正常时间段，且不能存在重叠） 适用岗位津贴规则*/
	 public static Integer differDay(Date leftStartDate, Date leftEndDate,  
			 Date rightStartDate, Date rightEndDate) {  
		 Integer dayNum=0;
		 try {
			 if(leftEndDate==null){
				leftEndDate=parseDate("9999-12-31");
			 }
			 if(rightEndDate==null){
				 rightEndDate=parseDate("9999-12-31");
			 }
		 } catch (ParseException e) {
			 e.printStackTrace();
		 }
		
		if(((leftEndDate.getTime() >= rightStartDate.getTime())   
	                    && leftStartDate.getTime() <= rightEndDate.getTime())){
			return null;//如果存在重叠就不比较
		} 
		Date [] date={leftStartDate,leftEndDate,rightStartDate, rightEndDate}; 
		//利用冒泡排序来计算
		for(int i = 0 ; i < date.length-1 ; i++){
			   for(int j = i+1 ; j < date.length ; j++){
			    Date temp ;
			    if(date[i].getTime() > date[j].getTime()){
			     temp = date[j];
			     date[j] = date[i];
			     date[i] = temp;
			    }
			   }
		}
		long day = (date[2].getTime()-date[1].getTime())/(24*60*60*1000)>0 ? (date[2].getTime()-date[1].getTime())/(24*60*60*1000):
				   (date[2].getTime()-date[1].getTime())/(24*60*60*1000); 
		dayNum=Integer.parseInt(day+"");
	        return dayNum;
	    } 
	 
	 /**
	  * 季度转月度
	  * @param quarter  第几季度
	  * @param isQuarterStart  true:计算开始月，false：计算结束月
	  * @return
	  */
	 public static int getQuarterInMonth(int quarter, boolean isQuarterStart) {  
	        int months[] = { 1, 4, 7, 10 };  
	        int month = 0;
	        if (!isQuarterStart) {  
	            months = new int[] { 3, 6, 9, 12 };  
	        }  
	        switch(quarter){
	        case 1:
	        	month = months[0];
	        	break;
	        case 2:
	        	month = months[1];
	        	break;
	        case 3:
	        	month = months[2];
	        	break;
	        case 4:
	        	month = months[3];
	        	break;
	        }
	        return month;
	    }  
}
